const dataSourceMixin = {
  data() {
    return {
      microservices: "",
      resource: "",
      showOverflow: false,
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
      dataSourceForm: {},
      dataSourceFormItems: [],
      dataSourceFormRule: {},
      data: [],
      columns: [],
      componentView: "",
      commonSearch: { AND: [] },
      propertyNames: [],
      sort: "insertTime asc",
    };
  },
  methods: {
    deleteOperation() {
      let items = this.componentView.getSelectRecords();
      if (items && items.length == 0) {
        this.$message.warning("未选择数据，不可删除");
        return;
      }
      this.$confirm("此操作将永久删除该相关信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$server()
            .batchDelete(this.microservices, this.resource, items)
            .then(() => {
              this.$message.success("删除成功");
              this.loadData();
            })
            .catch((response) => {
              this.$message.error(response.message);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      this.componentView.clearCheck();
    },
    loadData() {
      this.$server()
        .query(
          this.microservices,
          this.resource,
          this.commonSearch,
          this.sort,
          { current: this.page.currentPage, size: this.page.pageSize },
          true
        )
        .then((response) => {
          this.page.total = response.total;
          this.data = response.list;
        })
        .catch((response) => {
          this.$message.error(response.data.message);
        });
    },
    handlePageChange({ currentPage, pageSize }) {
      this.page.currentPage = currentPage;
      this.page.pageSize = pageSize;
      this.loadData();
    },
    resetQuery() {
      const clearObject = (obj) => {
        for (const key in obj) {
          if (Object.prototype.hasOwnProperty.call(obj, key)) {
            if (typeof obj[key] === "object" && obj[key] !== null) {
              clearObject(obj[key]);
            } else {
              obj[key] = "";
            }
          }
        }
      };
      clearObject(this.dataSourceForm);

      this.page.currentPage = 1;
      this.commonSearch = { AND: [] };
      this.loadData();
    },
    handleQuery() {
      this.page.currentPage = 1;
      this.commonSearch = { AND: [] };
      this.dataSourceFormItems.forEach((item) => {
        const { model, type, dateType, notUse } = item; // 添加 dateType
        const value = this.getValueByModel(model);

        if (value !== null && value !== undefined && value !== "") {
          let query;

          if (notUse) {
            return;
          }

          switch (type) {
            case "input":
              query = [model, "LIKE", `%${value}%`];
              break;
            case "select":
              query = [model, "=", value];
              break;
            case "date":
              if (dateType === "daterange") {
                query = [model, "BETWEEN '%s' AND '%s'", value]; // 修改为 BETWEEN 操作符
              } else {
                query = [model, ">", value];
              }
              break;
            // 添加其他类型的处理逻辑
          }

          this.commonSearch.AND.push(query);
        }
      });

      // 添加公共条件
      this.commonSearch.AND.push(["isDelete", "=", 0]);

      this.page.currentPage = 1;
      this.loadData();
    },
    getValueByModel(model) {
      const keys = model.split(".");
      let value = this.dataSourceForm;

      keys.forEach((key) => {
        // 检查是否为对象类型
        if (typeof value === "object" && value !== null) {
          value = value[key];
        } else {
          value = null;
        }
      });

      return value;
    },
    getPropertyName(property) {
      return this.propertyNames.find((item) => item.name === property)?.value;
    },
  },
};

export default dataSourceMixin;
